<template>
  <el-card class="box-card" style="height: 900px">
    <div slot="header">
      <el-menu default-active="1" active-text-color="#FF0000" class="el-menu-demo" mode="horizontal">
        <el-menu-item index="1">修改密码</el-menu-item>
      </el-menu>
    </div>
    <div style="width: 400px">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="手机号" prop="uphone">
          <el-input disabled type="text"  v-model="ruleForm.uphone"></el-input>
        </el-form-item>
        <el-form-item label="重置密码" prop="upassword">
          <el-input type="password" v-model="ruleForm.upassword1"  show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="upassword">
          <el-input type="password" v-model="ruleForm.upassword2" show-password ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
  import qs from "qs"

  export default {

    name: "Changepassword",
    data() {

      var validatePass3 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.upassword1 !== '') {
            this.$refs.ruleForm.validateField('oldpass');
          }
          callback();
        }
      };
     var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.upassword2) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          upassword:'',
          upassword1:'',
          uphone:'',
          upassword2:'',
        },
        rules: {
          upassword2: [
            { validator: validatePass, trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
          upassword1: [
            { validator: validatePass3, trigger: 'blur'},
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
     /* submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            /!*后端判定手机号是否是教师
            *   如果是教师，更改teacher表中对应手机号密码 更改student表密码
            *   不是的话，只更改student中的密码，密码同意加密 使用 MD5 2次 盐 yan
            * *!/
            this.$axios2.post("/student/stu/user/updStudent",qs.stringify(this.ruleForm)).then(r=>{
              alert(JSON.stringify(r.data));
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },*/
     submitForm(){
       if(this.ruleForm.upassword1===this.ruleForm.upassword2){
         this.ruleForm.upassword=this.ruleForm.upassword1;
         this.$axios2.post("/student/stu/user/updStudent",qs.stringify(this.ruleForm)).then(r=>{
           if (r.data){
                  alert("修改成功");
             window.sessionStorage.clear();
             this.$router.push('/login2')
             this.$message.error('请重新登录');
                }
         })
       }

     },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },created(){
      let uid =sessionStorage.getItem("uid");
      this.$axios2.get("/student/stu/user/slectAll?uid="+uid).then(r => {
        this.ruleForm=r.data;
      })
    }
  }
</script>
<style>
  .el-card__header{
    border-bottom: 0px;
  }
</style>
